package com.hospital;

import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.hospital.pony.adapter.ChoiceTypeAdapter;
import com.hospital.pony.base.BaseActivity;
import com.hospital.pony.config.Consts;
import com.hospital.pony.model.DeportmentModel;
import com.hospital.pony.model.ResponseEntry;
import com.hospital.pony.util.LoadingDialog;
import com.hospital.pony.util.ToastUtil;
import com.hospital.pony.view.DialogListMsg;

import net.tsz.afinal.http.AjaxParams;

import java.util.ArrayList;
import java.util.List;


public class RegisterCreatActivity extends BaseActivity {
    // 标题
    private TextView mTvTitle;
    // 返回
    private ImageView mIvBack;
    private Button mSubmit;

    private EditText metName;
    private EditText metPhone;
    private EditText metPswd;

    private int choiceType = 1;
    private RadioGroup mrbChoice;
    private RadioButton mrbDoctor = null;
    private RadioButton mrbPatient = null;
    private DialogListMsg dialogListMsg;
    private List<DeportmentModel> listType = new ArrayList<DeportmentModel>();
    private LinearLayout mllshow;
    private int posNumber;

    private RadioGroup mrbzhichengChoice;
    private RadioButton mrb1 = null;
    private RadioButton mrb2 = null;
    private RadioButton mrb3 = null;
    private String personType = "主治医师";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_creat_register);
        initWidget();
        initData();
    }

    @Override
    public void onClick(View v) {
        if(v.getId()==R.id.mIvBack){
            finish();
        }
        if(v.getId()==R.id.mBtnChoice){
            dialogListMsg.Show();
        }
        if(v.getId()==R.id.mSubmit){
            createTopicPost(true);
        }
    }

    private Button mBtnChoice;

    @Override
    public void initWidget() {

        mrbzhichengChoice = (RadioGroup) findViewById(R.id.mrbzhichengChoice);
        mrb1 = (RadioButton) findViewById(R.id.mrb1);
        mrb2 = (RadioButton) findViewById(R.id.mrb2);
        mrb3 = (RadioButton) findViewById(R.id.mrb3);

        mllshow = (LinearLayout) findViewById(R.id.mllshow);
        mBtnChoice = (Button) findViewById(R.id.mBtnChoice);
        mBtnChoice.setOnClickListener(this);


        dialogListMsg = new DialogListMsg(this);
        dialogListMsg.setTitle().setText("请选择所属科室");

        mrbChoice = (RadioGroup) findViewById(R.id.mrbChoice);
        mrbDoctor = (RadioButton) findViewById(R.id.mrbDoctor);
        mrbPatient = (RadioButton) findViewById(R.id.mrbPatient);
        metName = (EditText) findViewById(R.id.metName);
        metPhone = (EditText) findViewById(R.id.metPhone);
        metPswd = (EditText) findViewById(R.id.metPswd);

        mdialog = new LoadingDialog(this, "上传图片...");
        mSubmit = (Button) findViewById(R.id.mSubmit);
        mIvBack = (ImageView) findViewById(R.id.mIvBack);
        mTvTitle = (TextView) findViewById(R.id.mTvTitle);
        mTvTitle.setText("注册");
        mIvBack.setVisibility(View.VISIBLE);
        mIvBack.setOnClickListener(this);
        mSubmit.setOnClickListener(this);

    }

    @Override
    public void initData() {

        mrbzhichengChoice.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == mrb1.getId()) {
                    personType = "主治医师";
                } else if (checkedId == mrb2.getId()) {
                    personType = "主任医师";
                }else if (checkedId == mrb3.getId()) {
                    personType = "副主任医师";
                }
            }
        });


        mrbChoice.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == mrbDoctor.getId()) {
                    choiceType = 1;
                    mllshow.setVisibility(View.GONE);
                } else if (checkedId == mrbPatient.getId()) {
                    choiceType = 2;
                    mllshow.setVisibility(View.VISIBLE);
                }
            }
        });


        dialogListMsg.show_listview().setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
                posNumber = pos;
                mBtnChoice.setText(listType.get(pos).getDepartmentName());
                dialogListMsg.Close();
            }
        });

        dialogListMsg.submit_no().setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                dialogListMsg.Close();
            }
        });
        listPhoneTypeMessage(false);

    }


    private void createTopicPost(boolean isShow) {
        AjaxParams params = new AjaxParams();
        params.put("uname", metName.getText().toString());
        params.put("upswd", metPswd.getText().toString());
        params.put("uphone", metPhone.getText().toString());
        params.put("utype", choiceType + "");
        params.put("udepartID", listType.get(posNumber).getDepartmentId() + "");
        params.put("udepartName",listType.get(posNumber).getDepartmentName() + "");
        params.put("uLeave", personType + "");
        httpPost(Consts.URL + Consts.APP.RegAction, params, Consts.actionId.resultCode, isShow, "正在注册...");
    }

    private void listPhoneTypeMessage(boolean isShow) {
        AjaxParams params = new AjaxParams();
        params.put("action_flag", "listMesagePhoneDepartment");
        httpPost(Consts.URL + Consts.APP.HospitalAction, params, Consts.actionId.resultFlag, isShow, "正在注册...");
    }

    @Override
    protected void callBackSuccess(ResponseEntry entry, int actionId) {
        super.callBackSuccess(entry, actionId);
        switch (actionId) {
            case Consts.actionId.resultCode:
                ToastUtil.show(RegisterCreatActivity.this, entry.getRepMsg());
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        finish();
                    }
                }, 2000);
                break;
            case Consts.actionId.resultFlag:
                if (null != entry.getData() && !TextUtils.isEmpty(entry.getData())) {

                    String jsonMsg = entry.getData().substring(1, entry.getData().length() - 1);
                    if (null != jsonMsg && !TextUtils.isEmpty(jsonMsg)) {
                        listType.clear();
                        listType = mGson.fromJson(entry.getData(), new TypeToken<List<DeportmentModel>>() {
                        }.getType());


                        ChoiceTypeAdapter choiceStudentAdapter = new ChoiceTypeAdapter(this);
                        choiceStudentAdapter.setData(listType);
                        dialogListMsg.show_listview().setAdapter(choiceStudentAdapter);

                    }
                }
                break;


        }
    }

    @Override
    protected void callBackAllFailure(String strMsg, int actionId) {
        super.callBackAllFailure(strMsg, actionId);
        ToastUtil.show(this, strMsg);

    }
}
